<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.blurry" clearable size="small" placeholder="床位号、床位名称" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
          <el-form-item label="房间号" prop="roomNo">
            <el-input v-model="form.roomNo" style="width: 340px;" />
          </el-form-item>
          <el-form-item label="房间名称" prop="roomName">
            <el-input v-model="form.roomName" style="width: 340px;" />
          </el-form-item>
          <el-form-item label="床位号" prop="bedNo">
            <el-input v-model="form.bedNo" style="width: 340px;" />
          </el-form-item>
          <el-form-item label="床位名称" prop="bedName">
            <el-input v-model="form.bedName" style="width: 340px;" />
          </el-form-item>
          <el-form-item label="照护区名称" prop="nurscareRegnName">
            <el-input v-model="form.nurscareRegnName" disabled style="width: 340px;">
              <el-button slot="append" icon="el-icon-search" title="选择照护区" @click="getAllRegn" />
            </el-input>
          </el-form-item>
          <el-form-item label="照护区号" prop="nurscareRegnNo">
            <el-input v-model="form.nurscareRegnNo" disabled style="width: 340px;" />
          </el-form-item>
          <el-form-item label="护理区类型" prop="nurscareRegnType">
            <el-select v-model="form.nurscareRegnType" filterable disabled placeholder="请选择" style="width: 340px;">
              <el-option
                v-for="item in dict.PRIC_NURSCARE_REGN_TYPE"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="照护区ID" prop="longtermOrgNurscareRegnId">
            <el-input v-model="form.longtermOrgNurscareRegnId" disabled style="width: 340px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="nurscareRegnName" label="照护区名称" />
        <el-table-column prop="nurscareRegnType" label="护理区类型">
          <template slot-scope="scope">
            {{ dict.label.PRIC_NURSCARE_REGN_TYPE[scope.row.nurscareRegnType] }}
          </template>
        </el-table-column>
        <el-table-column prop="roomNo" label="房间号" />
        <el-table-column prop="roomName" label="房间名称" />
        <el-table-column prop="bedNo" label="床位号" />
        <el-table-column prop="bedName" label="床位名称" />
        <el-table-column prop="crteTime" label="数据创建时间" />
        <el-table-column prop="valiFlag" label="有效标志">
          <template slot-scope="scope">
            {{ dict.label.PRIC_VALI_FLAG[scope.row.valiFlag] }}
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','pricBed:edit','pricBed:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <!--Drawer 抽屉，隐藏异名库列表-->
      <el-drawer
        title="照护区"
        :visible.sync="table"
        direction="rtl"
        size="50%"
      >
        <el-table ref="tableDrawer" v-loading="crud.loading" :data="allRegn" height="100%">
          <el-table-column type="index" :index="indexMethod" label="序号" align="center" />
          <el-table-column prop="nurscareRegnName" label="照护区名称" />
          <el-table-column prop="nurscareRegnType" label="照护区类型">
            <template slot-scope="scope">
              {{ dict.label.PRIC_NURSCARE_REGN_TYPE[scope.row.nurscareRegnType] }}
            </template>
          </el-table-column>
          <el-table-column prop="roomCnt" label="房间数" />
          <el-table-column prop="bedVal" label="床位数" />
          <el-table-column prop="nurscarePsnCnt" label="护理员人数" />
          <el-table-column prop="dscr" label="描述" />

          <el-table-column
            label="操作"
            width="120"
            align="center"
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                @click.native.prevent="selectRow(scope.$index, allRegn)"
              >
                确定
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-drawer>
    </div>
  </div>
</template>

<script>
import crudPricBed from '@/api/business/pricBed'
import crudPricRegn from '@/api/business/pricRegn'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { pricBedId: null, nurscareOrgNo: null, nurscareOrgId: null, roomNo: null, roomName: null, bedNo: null, bedName: null, nurscareRegnName: null, nurscareRegnNo: null, nurscareRegnType: null, longtermOrgNurscareRegnId: null, rid: null, crteTime: null, updtTime: null, crterId: null, crterName: null, crteOptinsNo: null, opterId: null, opterName: null, optTime: null, optinsNo: null, admdvs: null, valiFlag: null, dataVer: null, ywlx: null, lrr: null, lrrxm: null, lrbm: null, lrbmmc: null, lrsj: null, gxr: null, gxrxm: null, gxbm: null, gxbmmc: null, gxsj: null, sfyx: null, bz: null }
export default {
  name: 'PricBed',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['PRIC_NURSCARE_REGN_TYPE', 'PRIC_VALI_FLAG'],
  cruds() {
    return CRUD({ title: '床位维护', url: 'api/pricBed', idField: 'pricBedId', sort: 'pricBedId,desc', crudMethod: { ...crudPricBed }})
  },
  data() {
    return {
      allRegn: [],
      table: false,
      permission: {
        add: ['admin', 'pricBed:add'],
        edit: ['admin', 'pricBed:edit'],
        del: ['admin', 'pricBed:del']
      },
      rules: {
        roomNo: [
          { required: true, message: '房间号必填', trigger: 'blur' }
        ],
        roomName: [
          { required: true, message: '房间名称必填', trigger: 'blur' }
        ],
        bedNo: [
          { required: true, message: '床位号必填', trigger: 'blur' }
        ],
        bedName: [
          { required: true, message: '床位名称必填', trigger: 'blur' }
        ],
        nurscareRegnName: [
          { required: true, message: '照护区名称必填', trigger: 'blur' }
        ],
        nurscareRegnNo: [
          { required: true, message: '照护区号必填', trigger: 'blur' }
        ],
        nurscareRegnType: [
          { required: true, message: '护理区类型必填', trigger: 'blur' }
        ],
        longtermOrgNurscareRegnId: [
          { required: true, message: '照护区ID必填', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    /**
     * 异名库的序号
     * @param index
     * @returns {*}
     */
    indexMethod(index) {
      return index + 1
    },
    /**
     * 确认选择的项目
     * @param index
     * @param rows
     */
    selectRow(index, rows) {
      // console.log(rows[index])
      const ymk = rows[index]
      console.log(ymk)

      this.table = false
      this.form.nurscareRegnName = ymk.nurscareRegnName
      this.form.nurscareRegnNo = ymk.nurscareRegnNo
      this.form.nurscareRegnType = ymk.nurscareRegnType
      this.form.longtermOrgNurscareRegnId = ymk.longtermOrgNurscareRegnId
    },
    // 获取弹窗内照护区数据
    getAllRegn() {
      this.table = true
      crudPricRegn.getAllRegn().then(res => {
        this.allRegn = res.content
        console.log('this.allRegn:', this.allRegn)
      }).catch(() => { })
    }
  }
}
</script>

<style scoped>

</style>
